package com.tianji.learning.mq.listener;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.QA_LIKED_TIMES_KEY;

/**
 * <p>
 * 监听点赞信息
 * </p>
 *
 * @author taohongrun
 * @since 2025/2/7
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RemarkChangeListener {
    private final IInteractionReplyService replyService;
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "qa.liked.times.queue")
            ,exchange = @Exchange(value = LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC)
            ,key = QA_LIKED_TIMES_KEY))
    public void listenReplyLikedTimesChange(List<LikedTimesDTO> dtoList){
        if(!dtoList.isEmpty()){
            List<InteractionReply> l = new ArrayList<>();
            for(LikedTimesDTO dto : dtoList){
                log.debug("监听到回答或评论{}的点赞数变更:{}", dto.getBizId(), dto.getLikedTimes());
                InteractionReply r = new InteractionReply();
                r.setId(dto.getBizId());
                r.setLikedTimes(dto.getLikedTimes());
                r.setUpdateTime(LocalDateTime.now());
                l.add(r);
            }
            replyService.updateBatchById(l);
        }

    }
}
